package iscript.uiFramework.components.form 
{
	import flash.display.DisplayObjectContainer;
	import iscript.uiFramework.components.button.IButton;
	import iscript.uiFramework.components.button.ImageButton;
	import iscript.uiFramework.components.UIComponent;
	import iscript.uiFramework.data.ComboBoxModel;
	import iscript.uiFramework.event.UIComponentEvent;
	import iscript.uiFramework.UIFramework;
	import iscript.uiFramework.util.UIComponentUtil;

	
	
	[Event(name="CellRender_Select", type = "iscript.uiFramework.event.ComboBoxEvent")]
	
	public class ComboBox extends  UIComponent 
	{
		
		private var _textInput:TextInput;
		private var _dropButton:ImageButton;
		private var _comboBoxPanel:ComboBoxPanel;
		private var _storeData:Vector.<ComboBoxModel>
		private var _rowCount:int;
		
		public function ComboBox() 
		{
			_rowCount = 5;
		   	_storeData = new Vector.<ComboBoxModel>();
			super();
		}
		 
		override protected function initialize():void 
		{
			_textInput = new TextInput();
			_textInput.editable = false;
			_textInput.addEventListener("focus_in",_dropLogic)
			_textInput.render(this);
			
			_dropButton = new ImageButton(
							UIFramework.resource.getBitmapData("GameLite.UIComponent." + this.theme + ".ComboboxButton.Normal"),
							UIFramework.resource.getBitmapData("GameLite.UIComponent." + this.theme + ".ComboboxButton.Hover"),
							UIFramework.resource.getBitmapData("GameLite.UIComponent."+this.theme+".ComboboxButton.Down")
							)
			_dropButton.render(this);	
			_textInput.height = _dropButton.height;
			this.width = 100;
			
			_dropButton.ClickHandler = _dropLogic;
			
			super.initialize();
		}
		
		private function _dropLogic(e:*):void {
			 if(_storeData.length>0){
				UIComponentUtil.floatTo(this, _comboBoxPanel);
			
				_comboBoxPanel.visible = !_comboBoxPanel.visible;
				_comboBoxPanel.width = this.width;
			 }
		}
		
		override public function set width(value:Number):void 
		{
			super.width = value;
			_textInput.width = value- _dropButton.width;
			_dropButton.x = _textInput.width;
		}
		override public function get width():Number 
		{
			return _textInput.width + _dropButton.width;
		}
		
		public function get storeData():Vector.<ComboBoxModel> 
		{
			return _storeData;
		}
		
		public function set storeData(value:Vector.<ComboBoxModel>):void 
		{
			if (value == null)
			value = new Vector.<ComboBoxModel>();
			_storeData = value;
			if (IsRender) {
				var prevX:Number = _comboBoxPanel.x;
				var prevY:Number = _comboBoxPanel.y;
				if (_comboBoxPanel && !_comboBoxPanel.Disposed)_comboBoxPanel.dispose();
					_comboBoxPanel = new ComboBoxPanel(this, _storeData);
					_comboBoxPanel.x = prevX;
					_comboBoxPanel.y = prevY;
					_comboBoxPanel.render();
			}
		}
		
		/**
		 * 显示行数
		 */
		public function get rowCount():int 
		{
			return _rowCount;
		}
		
		/**
		 * 显示行数
		 */
		public function set rowCount(value:int):void 
		{
			if(value<3) value=3
			_rowCount = value;
		}
		
		
		public function setText(value:String):void {
			  _textInput.text = value;
			}
		
		override public function render(container:DisplayObjectContainer = null):void 
		{
			_comboBoxPanel = new ComboBoxPanel(this,_storeData);
			_comboBoxPanel.visible = false;
			_comboBoxPanel.render();
			super.render(container);
		}
		
		
		
		
		
	}

}